#include "wifi_devinit.h"

#include "esp_err.h"
#include "esp_event.h"
#include "esp_wifi.h"
#include "freertos/event_groups.h"
#include "nvs_flash.h"
#include <stdbool.h>
#include "mdns.h"

static void NVS_Flash_INIT(bool value)
{
    if (value == false)
        return;
    esp_err_t ret = nvs_flash_init();
    if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND)
    {
        ESP_ERROR_CHECK(nvs_flash_erase());
        ret = nvs_flash_init();
    }
    ESP_ERROR_CHECK(ret);
}

static EventGroupHandle_t wifi_event_group;
const int                 CONNECTED_BIT = BIT0;
static void               event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data)
{
    if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START)
    {
        esp_wifi_connect();
    }
    else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED)
    {
        esp_wifi_connect();
        xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
    }
    else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP)
    {
        xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
    }
}

#define EXAMPLE_WIFI_SSID CONFIG_EXAMPLE_WIFI_SSID
static void WIFI_on_INIT()
{
    ESP_ERROR_CHECK(esp_netif_init());   // 初始化LwIP,创建核心任务并初始化LwIP相关流程
    /* 初始化事件 */
    ESP_ERROR_CHECK(esp_event_loop_create_default());

    esp_event_handler_instance_t instance_any_id;
    esp_event_handler_instance_t instance_got_id;

    ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL));
    ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL));

    // 初始化wifi驱动程序任务,并初始化WIFI驱动程序
    wifi_init_config_t WIFI_cfg = WIFI_INIT_CONFIG_DEFAULT();
    ESP_ERROR_CHECK(esp_wifi_init(&WIFI_cfg));
}
#define EXAMPLE_ESP_WIFI_SSID "ASUS_VPN"
#define EXAMPLE_ESP_WIFI_PASS "LY888888"
// #define EXAMPLE_ESP_WIFI_SSID "LY"
// #define EXAMPLE_ESP_WIFI_PASS "LY888888"
// #define EXAMPLE_ESP_WIFI_SSID "LY"
// #define EXAMPLE_ESP_WIFI_PASS "LY888888"
static void WIFI_configuration()
{
    esp_netif_t* sta_netif = esp_netif_create_default_wifi_sta();
    esp_netif_set_hostname(sta_netif, "祝工的神奇串口");
    // 配置wifi信息
    wifi_config_t wifi_config = {.sta = {
                                     .ssid               = EXAMPLE_ESP_WIFI_SSID,
                                     .password           = EXAMPLE_ESP_WIFI_PASS,
                                     .threshold.authmode = WIFI_AUTH_WPA2_PSK,
                                 }};
    // 设置wifi模式,配置wifi具体参数
    ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
    ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
}
static void WIFI_starer()
{
    esp_wifi_set_ps(WIFI_PS_NONE);   // 全速运行
    ESP_ERROR_CHECK(esp_wifi_start());
}
void WIFI_Init(void)
{
    // 使用事件组之前记得初始化
    wifi_event_group = xEventGroupCreate();
    /* 初始化wifi必须初始化nvs_flash */
    NVS_Flash_INIT(true);
    // 初始化
    WIFI_on_INIT();
    /* 配置 */
    WIFI_configuration();
    /* 启动 */
    WIFI_starer();
}